import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:sainbainu/common/widgets/widget.dart';

/// 媒体类型
enum MediaType { image, video }

/// 媒体集合每一项
class ImageVideoItem {
  final String src;
  final MediaType mediaType;

  ImageVideoItem({required this.src, required this.mediaType});
}

/// 组件宽度
double _boxSize = 0;

/// 间隔
const double gap = 10;

class ImageVideoGrid extends StatelessWidget {
  final List<String> imageLinkList;
  final String? videoLink;
  final EdgeInsetsGeometry padding;

  const ImageVideoGrid({
    Key? key,
    this.imageLinkList = const [],
    this.videoLink,
    this.padding = const EdgeInsets.all(0),
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (imageLinkList.isEmpty && (videoLink == null || videoLink!.isEmpty)) {
      return const SizedBox.shrink();
    }

    // 获取父组件限制最大宽度 就是我们组件的宽度
    return LayoutBuilder(builder: (BuildContext ctx, BoxConstraints bc) {
      _boxSize = bc.maxWidth;
      return Padding(
        padding: padding,
        child: _buildImageVideo(),
      );
    });
  }

  /// 渲染
  Widget _buildImageVideo() {
    // 没图有视频
    if (imageLinkList.isEmpty) {
      return AspectRatio(
        aspectRatio: 1 / 1,
        child: _buildVideoEntity(videoLink!),
      );
    }

    // 组装图片与视频的集合
    List<ImageVideoItem> imageVideoList = imageLinkList.map((item) {
      return ImageVideoItem(src: item, mediaType: MediaType.image);
    }).toList();

    if (videoLink != null && videoLink!.isNotEmpty) {
      imageVideoList.add(
        ImageVideoItem(src: videoLink!, mediaType: MediaType.video),
      );
    }

    switch (imageVideoList.length) {
      case 1:
        return _build1Item(imageVideoList);
      case 2:
        return _squareBox(_build2Item(imageVideoList));
      case 3:
        return _squareBox(_build3Item(imageVideoList));
      case 4:
        return _squareBox(_build4Item(imageVideoList));
      case 5:
        return _squareBox(_build4Item(imageVideoList));
      case 6:
        return _squareBox(_build5Item(imageVideoList));
      case 7:
        return _squareBox(_build5Item(imageVideoList));
      case 8:
        return _squareBox(_build5Item(imageVideoList));
      case 9:
        return _squareBox(_build6Item(imageVideoList));
      case 10:
        return _squareBox(_build6Item(imageVideoList));
      case 11:
        return _squareBox(_build6Item(imageVideoList));
      default:
        return _build1Item(imageVideoList);
    }
  }

  /// 组件容器
  Widget _squareBox(Widget child) {
    return SizedBox(
      width: _boxSize,
      height: _boxSize,
      child: child,
    );
  }

  /// 9、10、11个媒体
  Widget _build6Item(List<ImageVideoItem> imageVideoList) {
    return GridView.count(
      padding: const EdgeInsets.all(0),
      crossAxisCount: 3,
      childAspectRatio: 1 / 1,
      mainAxisSpacing: gap,
      crossAxisSpacing: gap,
      physics: const NeverScrollableScrollPhysics(),
      children: [
        _buildImageOrVideoEntity(imageVideoList[0]),
        _buildImageOrVideoEntity(imageVideoList[1]),
        _buildImageOrVideoEntity(imageVideoList[2]),
        _buildImageOrVideoEntity(imageVideoList[3]),
        _buildImageOrVideoEntity(imageVideoList[4]),
        _buildImageOrVideoEntity(imageVideoList.last),
        _buildImageOrVideoEntity(imageVideoList[5]),
        _buildImageOrVideoEntity(imageVideoList[6]),
        _buildImageOrVideoEntity(imageVideoList[7]),
      ],
    );
  }

  /// 6、7、8个媒体
  Widget _build5Item(List<ImageVideoItem> imageVideoList) {
    double smallSize = _boxSize / 3 - gap / 1.5;
    double bigSize = _boxSize - smallSize - gap;

    // ImageVideoItem imageVideoItem = imageVideoList.singleWhere((ImageVideoItem imageVideoItem) {
    //   return imageVideoItem.mediaType == MediaType.video;
    // }, orElse: () {
    //   return imageVideoList.last;
    // });

    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        SizedBox(
          width: smallSize,
          height: _boxSize,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              SizedBox(
                width: smallSize,
                height: smallSize,
                child: _buildImageOrVideoEntity(imageVideoList[0]),
              ),
              SizedBox(
                width: smallSize,
                height: smallSize,
                child: _buildImageOrVideoEntity(imageVideoList[1]),
              ),
              SizedBox(
                width: smallSize,
                height: smallSize,
                child: _buildImageOrVideoEntity(imageVideoList[2]),
              ),
            ],
          ),
        ),
        SizedBox(
          width: bigSize,
          height: _boxSize,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              SizedBox(
                width: bigSize,
                height: smallSize,
                child: _buildImageOrVideoEntity(imageVideoList[3]),
              ),
              SizedBox(
                width: bigSize,
                height: _boxSize - smallSize - gap,
                child: _buildImageOrVideoEntity(imageVideoList.last),
              ),
            ],
          ),
        ),
      ],
    );
  }

  /// 4、5个媒体
  Widget _build4Item(List<ImageVideoItem> imageVideoList) {
    double smallSize = _boxSize / 3 - gap / 1.5;
    double bigSize = _boxSize - smallSize - gap;
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        SizedBox(
          width: smallSize,
          height: _boxSize,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              SizedBox(
                width: smallSize,
                height: smallSize,
                child: _buildImageOrVideoEntity(imageVideoList[0]),
              ),
              SizedBox(
                width: smallSize,
                height: smallSize,
                child: _buildImageOrVideoEntity(imageVideoList[1]),
              ),
              SizedBox(
                width: smallSize,
                height: smallSize,
                child: _buildImageOrVideoEntity(imageVideoList[2]),
              ),
            ],
          ),
        ),
        SizedBox(
          width: bigSize,
          height: _boxSize,
          child: _buildImageOrVideoEntity(imageVideoList.last),
        ),
      ],
    );
  }

  /// 3个媒体
  Widget _build3Item(List<ImageVideoItem> imageVideoList) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            SizedBox(
              width: _boxSize / 2 - gap / 2,
              height: _boxSize / 2 - gap / 2,
              child: _buildImageOrVideoEntity(imageVideoList[0]),
            ),
            SizedBox(
              width: _boxSize / 2 - gap / 2,
              height: _boxSize / 2 - gap / 2,
              child: _buildImageOrVideoEntity(imageVideoList[1]),
            ),
          ],
        ),
        SizedBox(
          width: _boxSize,
          height: _boxSize / 2 - gap / 2,
          child: _buildImageOrVideoEntity(imageVideoList[2]),
        ),
      ],
    );
  }

  /// 2个媒体
  Widget _build2Item(List<ImageVideoItem> imageVideoList) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        SizedBox(
          width: _boxSize / 2 - gap / 2,
          height: _boxSize,
          child: _buildImageOrVideoEntity(imageVideoList[0]),
        ),
        SizedBox(
          width: _boxSize / 2 - gap / 2,
          height: _boxSize,
          child: _buildImageOrVideoEntity(imageVideoList[1]),
        ),
      ],
    );
  }

  /// 1个图
  Widget _build1Item(List<ImageVideoItem> imageVideoList) {
    return AspectRatio(
      aspectRatio: 1 / 1,
      child: _buildImageEntity(imageVideoList[0].src),
    );
  }

  /// 图片或视频
  Widget _buildImageOrVideoEntity(ImageVideoItem imageVideoItem) {
    if (imageVideoItem.mediaType == MediaType.image) {
      return _buildImageEntity(imageVideoItem.src);
    } else if (imageVideoItem.mediaType == MediaType.video) {
      return _buildVideoEntity(imageVideoItem.src);
    } else {
      return const SizedBox.shrink();
    }
  }

  /// 图片
  Widget _buildImageEntity(String url) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(6),
      child: CustomImage(url: url),
    );
  }

  /// 视频
  Widget _buildVideoEntity(String url) {
    return Stack(
      alignment: Alignment.center,
      fit: StackFit.expand,
      children: [
        ClipRRect(
          borderRadius: BorderRadius.circular(6),
          child: CustomImage(url: url),
        ),
        Center(
          child: Image.asset(
            'assets/images/play.png',
            width: 25,
            height: 25,
          ),
        ),
      ],
    );
  }
}
